package com.yuantu.judiciary.serviceimpl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.yuantu.judiciary.common.SysOnOffState;
import com.yuantu.judiciary.dao.*;
import com.yuantu.judiciary.exception.enums.AccountExceptionEnum;
import com.yuantu.judiciary.exception.enums.OrganizationExceptionEnum;
import com.yuantu.judiciary.exception.enums.UserExceptionEnum;
import com.yuantu.judiciary.exception.enums.VipExceptionEnum;
import com.yuantu.judiciary.model.domain.*;
import com.yuantu.judiciary.model.enums.AuthState;
import com.yuantu.judiciary.model.enums.OrganizationType;
import com.yuantu.judiciary.model.enums.UserType;
import com.yuantu.judiciary.model.form.AddUserForm;
import com.yuantu.judiciary.model.form.ModifyUserForm;
import com.yuantu.judiciary.model.form.SearchEnterFrom;
import com.yuantu.judiciary.model.form.SearchEnterUserFrom;
import com.yuantu.judiciary.model.vo.*;
import com.yuantu.judiciary.service.ICheckEnterpriseService;
import com.yuantu.judiciary.service.IOrganizationService;
import com.yuantu.judiciary.service.IOrganizationSourceService;
import com.yuantu.judiciary.utils.AssertUtil;
import com.yuantu.judiciary.utils.ExcelUtil;
import com.yuantu.judiciary.utils.StringUtil;
import com.yuantu.judiciary.utils.TimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 组织管理模块
 *
 * @author syw
 * @since 2023-08-12
 **/
@Service
@RequiredArgsConstructor
public class OrganizationSourceServiceImpl implements IOrganizationSourceService {


    private final IOrganizationSourceDao organizationSourceDao;

    private final IOrganizationDao organizationDao;


    @Override
    public PageVO<OrgSourceVO> searchOrgSourceInfos(String orgSource, Pageable pageable) {

        Page<OrganizationSourceDO> orgSourcePage = organizationSourceDao.findInfosByOrgSource(orgSource, pageable);
        List<OrganizationSourceDO> content = orgSourcePage.getContent();
        if (CollectionUtils.isEmpty(content)) {
            return new PageVO<>(orgSourcePage.getTotalElements(), Collections.emptyList());
        }
        List<OrgSourceVO> result = new ArrayList<>();
        for (OrganizationSourceDO organizationSource : content) {
            OrgSourceVO source = new OrgSourceVO();
            source.setOrgSourceId(organizationSource.getId());
            source.setOrgSource(organizationSource.getOrgSource());
            source.setName(organizationSource.getName());
            source.setDiscount(organizationSource.getDiscount());
            source.setPerAuth(organizationSource.getPerAuth());
            source.setEntAuth(organizationSource.getEntAuth());
            source.setCreateTime(organizationSource.getCreateTime());
            result.add(source);
        }
        return new PageVO<>(orgSourcePage.getTotalElements(), result);
    }

    @Override
    public Boolean updateOrgSource(OrgSourceVO orgSource) {

        if (orgSource.getOrgSourceId() == null) {
            OrganizationSourceDO organizationSource = new OrganizationSourceDO();
            organizationSource.setOrgSource(orgSource.getOrgSource());
            organizationSource.setName(orgSource.getName());
            organizationSource.setDiscount(orgSource.getDiscount());
            organizationSource.setPerAuth(orgSource.getPerAuth());
            organizationSource.setEntAuth(orgSource.getEntAuth());
            organizationSourceDao.save(organizationSource);
        } else {
            OrganizationSourceDO existOrgSource = organizationSourceDao.findInfoByIdAndDeletedFalse(orgSource.getOrgSourceId());
            AssertUtil.assertTrue(existOrgSource != null, OrganizationExceptionEnum.ORGANIZATION_SOURCE_NOT_EXIST);
            BeanUtils.copyProperties(orgSource, existOrgSource);
            organizationSourceDao.save(existOrgSource);
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean removeOrgSource(Long orgSourceId) {
        List<OrganizationDO> organizations = organizationDao.findInfosByOrganizationSourceIdAndDeletedFalse(orgSourceId);
        AssertUtil.assertTrue(CollectionUtils.isEmpty(organizations), OrganizationExceptionEnum.ORGANIZATION_SOURCE_NOT_DELETED);
        organizationSourceDao.removeInfoById(orgSourceId);
        return true;
    }
}
